﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LevelEditor
{
    public static class NoiseGenerator
    {
        static Random random = new Random();

        public static NoiseMap Generate(int Width, int Height)
        {
            float[,] map = new float[Width, Height];

            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                    map[x, y] = (float)random.NextDouble();
            }
            List<float> points = new List<float>();
            for (int y = 1; y < Height - 1; y++)
            {
                for (int x = 1; x < Width - 1; x++)
                {
                    Smooth(x, y, map);
                    Smooth(x, y, map);
                    Smooth(x, y, map);
                }
            }

            return new NoiseMap(map);
        }

        private static void Smooth(int x, int y, float[,] map)
        {
            float avg = 0.0f;
            avg += map[(x - 1), (y - 1)];
            avg += map[(x), (y - 1)];
            avg += map[(x + 1), (y - 1)];
            avg += map[(x - 1), y];
            avg += map[x, y];
            avg += map[(x + 1), y];
            avg += map[(x - 1), (y + 1)];
            avg += map[x, (y + 1)];
            avg += map[(x + 1), (y + 1)];
            avg /= 9.0f;

            map[x, y] = avg;
        }

        private static float Noise()
        {
            return (float)random.NextDouble();
        }
    }
}